(function (vue, $, name) {
  vue.component(name, {
    template:
            '<div>\
                <el-dialog-x title="下一步骤(单选)" :visible.sync="dialogFormVisible" height="370px" width="700px" @close="beforeClose">\
                    <el-form ref="nextNodeForm" :model="nextNodeForm" label-width="110px" :inline="true">\
                        <el-row>\
                            <el-col :span="4">\
                                &nbsp;\
                            </el-col>\
                            <el-col :span="20">\
                                <table>\
                                    <tbody>\
                                        <tr v-for="(item,index) in nextNodeList">\
                                            <td>\
                                                <el-radio :label="item.nodeId" v-model="radioModel" @change.native="onAgreeRadioNodeSelect">{{item.nodeName}}</el-radio>\
                                            </td>\
                                            <td>\
                                                <el-input v-model="item.userNames" placeholder="" size="small" :disabled="disabled"></el-input>\
                                            </td>\
                                            <td>\
                                                <el-button size="small" v-if="item.selected==\'1\'" :disabled="item.selected==\'0\'" @click="openNodeUsersFn(item.nodeId,index)">选择人员</el-button>\
                                            </td>\
                                        </tr>\
                                    </tbody>\
                                </table>\
                            </el-col>\
                        </el-row>\
                    </el-form>\
                    <div slot="footer" align="center">\
                        <el-button type="primary" @click="dialogFormVisible = false">关闭</el-button>\
                        <el-button type="primary" @click="saveCreateFn()">确 定</el-button>\
                    </div>\
                </el-dialog-x>\
                <el-dialog-x title="下一步骤(多选)" :visible.sync="dialogFormVisibleCheck" height="370px" width="700px" @close="beforeClose">\
                    <el-form ref="nextNodeFormCheck" :model="nextNodeFormCheck" label-width="110px" :inline="true">\
                        <el-row>\
                            <el-col :span="4">\
                                &nbsp;\
                            </el-col>\
                            <el-col :span="20">\
                                <table>\
                                    <tbody>\
                                        <tr v-for="(item,index) in nextNodeList">\
                                            <td>\
                                                <el-checkbox :label="item.nodeId" true-label="1" false-label="0" v-model="item.selected">{{item.nodeName}}</el-checkbox>\
                                            </td>\
                                            <td>\
                                                <el-input v-model="item.userNames" placeholder="" size="small" :disabled="disabled"></el-input>\
                                            </td>\
                                            <td>\
                                                <el-button v-if="item.selected==\'1\'" size="mini" :disabled="item.selected==\'0\'" @click="openNodeUsersFn(item.nodeId,index)">选择人员</el-button>\
                                            </td>\
                                        </tr>\
                                    </tbody>\
                                </table>\
                            </el-col>\
                        </el-row>\
                    </el-form>\
                    <div slot="footer" align="center">\
                        <el-button type="primary" @click="dialogFormVisibleCheck = false">关闭</el-button>\
                        <el-button type="primary" @click="saveCreateFn()">确 定</el-button>\
                    </div>\
                </el-dialog-x>\
                <el-dialog-x title="节点处理人(单选)" :visible.sync="dialogVisibleNodeUser" height="360px" width="550px">\
                    <el-table-x ref="nodeUserList" :data-url="urls.wfGetNodeUsers" :pageable=true request-type="POST" :default-load=false :data-params="nodeUserParams"\
                        :table-columns="nodeUserTableColumns">\
                    </el-table-x>\
                    <div slot="footer" align="center">\
                        <el-button type="primary" @click="returnNodeUser">选取返回</el-button>\
                    </div>\
                </el-dialog-x>\
                <el-dialog-x title="节点处理人(多选)" :visible.sync="dialogVisibleNodeUserCheck" height="360px" width="550px">\
                    <el-table-x ref="nodeUserCheckList" :checkbox=true :pageable=true :data-url="urls.wfGetNodeUsers" request-type="POST" :default-load=false\
                        :data-params="nodeUserParams" :table-columns="nodeUserTableColumns">\
                    </el-table-x>\
                    <div slot="footer" align="center">\
                        <el-button type="primary" @click="returnNodeUserCheck">选取返回</el-button>\
                    </div>\
                </el-dialog-x>\
                <el-dialog-x title="委托模式" :visible.sync="dialogVisibleAgent" height="360px" width="550px">\
                    <el-radio-group v-model="agent" @change="agentChange">\
                        <el-row v-for="(item,index) in agentList" :key="item.key">\
                            <el-col>\
                                <el-radio :label="item.key" :key="item.value">{{item.value}}</el-radio>\
                            </el-col>\
                        </el-row>\
                    </el-radio-group>\
                    <div slot="footer" align="center">\
                        <el-button type="primary" @click="saveAgent()">确 定</el-button>\
                    </div>\
                </el-dialog-x>\
            </div>',
    props: {
      commonParams: {
        type: Object,
        required: true
      },
      echainServerName: {
        type: String,
        default: backend.echainService
      }
    },
    data: function () {
      var me = this;
      return {
        nodeRouterType: '',
        wfSign: '',
        radioModel: '',
        checkList: [],
        nodeId: '',
        instanceId: '',
        applType: '',
        logicseq: '',
        index: '',
        agent: '0',
        agentModel: '0',
        users: '',
        disabled: true,
        nextNodeForm: {
          radioModel: '',
          radioInput: ''
        },
        nextNodeFormCheck: {
          checkModel: '',
          checkInput: ''
        },
        nextNodeList: [],
        exv10: '',
        agentList: [
          { key: '0', value: '代办人办理' },
          { key: '1', value: '办理人代人都可以办理' },
          { key: '2', value: '原办理人办理' }
        ],
        urls: {
          wfInit: me.echainServerName + '/api/joincore/wfInit',
          wfGetInstanceInfo: me.echainServerName + '/api/joincore/wfGetInstanceInfo',
          wfSave: me.echainServerName + '/api/joincore/wfSave',
          wfGetNextNodes: me.echainServerName + '/api/joincore/wfGetNextNodes',
          wfGetNodeUsers: me.echainServerName + '/api/joincore/wfGetNodeUsers',
          wfCheckAgent: me.echainServerName + '/api/joincore/wfCheckAgent',
          wfComplete: me.echainServerName + '/api/joincore/wfComplete'
        },
        nodeUserParams: {},
        nextNodeUserForTip: '',
        dialogFormVisible: false,
        dialogFormVisibleCheck: false,
        dialogVisibleNodeUser: false,
        dialogVisibleNodeUserCheck: false,
        dialogVisibleAgent: false,
        nodeUserTableColumns: [
          { label: '办理人ID', prop: 'loginCode', resizable: true },
          { label: '办理人名称', prop: 'userName', resizable: true }
        ]
      };
    },
    methods: {
      beforeClose: function () {
        this.nextNodeList = [];
        this.radioModel = '';
        this.checkList = [];
        this.agent = '0';
        this.$emit('afterclose');
      },
      wfInit: function (wfInitData) {
        var me = this;
        var comitData = wfInitData;
        me.exv10 = comitData.exv10;
        yufp.extend(comitData, me.commonParams);
        yufp.service.request({// 初始化流程
          url: me.urls.wfInit,
          data: wfInitData,
          method: 'POST',
          callback: function (code, message, response) {
            if (response.data != null) {
              comitData.instanceId = response.data.instanceId;
              comitData.nodeId = response.data.nodeId;
              me.nodeId = response.data.nodeId;
              me.instanceId = response.data.instanceId;
              yufp.service.request({// 获取实例信息
                url: me.urls.wfGetInstanceInfo,
                data: comitData,
                method: 'POST',
                callback: function (code, message, response) {
                  me.nodeRouterType = response.data.nodeRouterType;
                  me.wfSign = response.data.wfSign;
                  if (response.data != null) {
                    comitData.commentSign = '10';
                    comitData.commentContent = '发起流程';
                    yufp.service.request({// 保存流程意见
                      url: me.urls.wfSave,
                      data: comitData,
                      method: 'POST',
                      callback: function (code, message, response) {
                        if (response.data != null) {
                          me.openNodeFn();
                        } else {
                          me.$message({ message: '保存流程意见失败', type: 'error' });
                        }
                      }
                    });
                  } else {
                    me.$message({ message: '获取实例信息失败', type: 'error' });
                  }
                }
              });
            } else {
              me.$message({ message: '初始化流程失败', type: 'error' });
            }
          }
        });
      },
      onAgreeRadioNodeSelect: function () {
        var me = this;
        for (var index = 0; index < me.nextNodeList.length; index++) {
          var node = me.nextNodeList[index];
          if (node.nodeId == me.radioModel) {
            node.selected = '1';
          } else {
            node.selected = '0';
          }
        }
      },
      agentChange: function (val) { // 委托模式
        this.agent = val;
      },
      openNodeFn: function () { // 节点选择
        var me = this;
        var param = {};
        yufp.extend(param, me.commonParams);
        param.nodeId = this.nodeId;
        param.instanceId = this.instanceId;
        yufp.service.request({
          url: me.urls.wfGetNextNodes,
          data: param,
          method: 'POST',
          callback: function (code, message, response) {
            if (response.data.length > 0) {
              for (var i = 0; i < response.data.length; i++) { // 获取节点列表
                var nextNode = {
                  nodeId: response.data[i].nodeId,
                  nodeName: response.data[i].nodeName,
                  selected: '0',
                  users: '',
                  userNames: ''
                };
                me.nextNodeList.push(nextNode);
              }
              if (me.nodeRouterType == '2' || me.nodeRouterType == '4') { // 判断节点是单选还是多选
                me.dialogFormVisibleCheck = true;
              } else {
                me.dialogFormVisible = true;
              }
            } else {
              me.$message({ message: '没有下一节点', type: 'error' });
            }
          }
        });
      },
      openNodeUsersFn: function (val, index) { // 下一节点处理人选择
        var me = this;
        var param = {};
        me.index = index;
        yufp.extend(param, me.commonParams);
        param.nodeId = val;
        param.instanceId = me.instanceId;
        yufp.service.request({
          url: me.urls.wfGetNodeUsers,
          data: param,
          method: 'POST',
          callback: function (code, message, response) {
            if (response.data.length >= 1) {
              if (response.data[0].isMulteit == '0') { // 判断节点处理人是单选还是多选
                me.dialogVisibleNodeUser = true;
                me.$nextTick(function () {
                  me.$refs.nodeUserList.remoteData(param);
                });
              } else {
                me.dialogVisibleNodeUserCheck = true;
                me.$nextTick(function () {
                  me.$refs.nodeUserCheckList.remoteData(param);
                });
              }
            } else {
              me.$message({ message: '没有下一节点处理人', type: 'error' });
            }
          }
        });
      },
      saveAgent: function () { // 保存委托模式
        var me = this;
        me.agentModel = me.agent;
        me.nextNodeList[me.index].users = me.users; 
        me.dialogVisibleNodeUser = false;
        me.dialogVisibleNodeUserCheck = false;
        me.dialogVisibleAgent = false;
      },
      returnNodeUser: function () { // 选取下一节点处理人（单选）
        var me = this;
        if (this.$refs.nodeUserList.selections.length !== 1) {
          this.$message({ message: '请选择一条数据!', type: 'warning' });
          return false;
        }
        var row = this.$refs.nodeUserList.selections[0];
        me.users = row.loginCode;
        me.nextNodeList[me.index].userNames = row.userName;
        me.nextNodeUserForTip = row.userName;
        var param = {};
        yufp.extend(param, me.commonParams);
        param.applType = me.applType;
        param.users = row.loginCode;
        yufp.service.request({
          url: me.urls.wfCheckAgent,
          data: param,
          method: 'POST',
          callback: function (code, message, response) {
            if (response.data == 1) { // 判断是否设置了委托
              me.dialogVisibleAgent = true;
            } else {
              me.nextNodeList[me.index].users = row.loginCode;
              me.dialogVisibleNodeUser = false;
            }
          }
        });
      },
      returnNodeUserCheck: function () { // 选取下一节点处理人（多选）
        var me = this;
        if (this.$refs.nodeUserCheckList.selections.length < 1) {
          this.$message({ message: '请至少选择一条数据!', type: 'warning' });
          return false;
        }
        var list = this.$refs.nodeUserCheckList.selections;
        var data = '';
        var userTemp = '';
        for (var i = 0; i < list.length; i++) {
          if (i < list.length - 1) {
            data = data + list[i].loginCode + ';';
            userTemp = userTemp + list[i].userName + ';';
          } else {
            data = data + list[i].loginCode;
            userTemp = userTemp + list[i].userName;
          }
        }
        me.nextNodeList[me.index].userNames = userTemp;
        me.nextNodeUserForTip = userTemp;
        me.users = data;
        var param = {};
        yufp.extend(param, me.commonParams);
        param.applType = me.applType;
        param.users = data;
        yufp.service.request({
          url: me.urls.wfCheckAgent,
          data: param,
          method: 'POST',
          callback: function (code, message, response) {
            if (response.data == 1) { // 判断是否设置了委托
              me.dialogVisibleAgent = true;
            } else {
              me.nextNodeList[me.index].users = data;
              me.dialogVisibleNodeUserCheck = false;
            }
          }
        });
      },
      saveCreateFn: function () { // 流程提交
        var me = this;
        var nextUsers = '';
        var nextNodeId = '';
        var nextNodeName = '';
        var nextNodeListTemp = [];
        if (me.nextNodeList != null) {
          for (var k = 0; k < me.nextNodeList.length; k++) {
            if (me.nextNodeList[k].selected == '1') {
              var option = {
                nodeId: me.nextNodeList[k].nodeId,
                nodeName: me.nextNodeList[k].nodeName,
                users: me.nextNodeList[k].users
              };
              nextNodeListTemp.push(option);
            }
          }
        }
        if (nextNodeListTemp) {
          for (var i = 0; i < nextNodeListTemp.length; i++) {
            if (i < nextNodeListTemp.length - 1) {
              nextUsers = nextUsers + nextNodeListTemp[i].users + '@';
              nextNodeId = nextNodeId + nextNodeListTemp[i].nodeId + '@';
              nextNodeName = nextNodeName + nextNodeListTemp[i].nodeName + '@';
            } else {
              nextUsers = nextUsers + nextNodeListTemp[i].users;
              nextNodeId = nextNodeId + nextNodeListTemp[i].nodeId;
              nextNodeName = nextNodeName + nextNodeListTemp[i].nodeName;
            }
          }
        }
        var param = {};
        yufp.extend(param, me.commonParams);
        param.nextNodeUser = nextUsers;
        param.nodeId = me.nodeId;
        param.instanceId = me.instanceId;
        param.nextNodeId = nextNodeId;
        param.entrustModel = me.agentModel;
        param.bizSeqNo = me.logicseq;
        param.applType = me.applType;
        param.wfSign = me.wfSign;
        param.exv10 = me.exv10;
        me.instanceInfo = param;
        if (!nextNodeId) {
          me.$message({message: '请选择下一节点', type: 'error'});
          return false;
        }
        if (!me.nextNodeUserForTip) {
          me.$message({message: '请选择下一节点处理人', type: 'error'});
          return false;
        }
        yufp.service.request({
          url: me.echainServerName + '/api/joincore/wfComplete',
          data: param,
          method: 'POST',
          callback: function (code, message, response) {
            if (response.data != null) {
              var resData = response.data;
              me.$confirm('流程发起成功，下一节点为:' + resData.nextNodeName + ' ( ' + resData.nextNodeId + ' )' + ',下一节点处理人为:' + me.nextNodeUserForTip + ' (' + resData.nextNodeUser + ')', '提示', {
                confirmButtonText: '确定',
                showCancelButton: false,
                type: 'success',
                center: true
              }).then(function () {
                me.radioModel = '';
                me.dialogFormVisible = false;
                me.dialogFormVisibleCheck = false;
                me.$emit('afterinit');
              });
            } else {
              me.$message({ message: '流程提交失败', type: 'error' });
            }
          }
        });
      }
    },
    mounted: function () {

    }

  });
}(Vue, yufp.$, 'yufp-wf-init'));